package cn.bbs.ba.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

import org.apache.commons.lang.StringUtils;

import cn.bbs.ba.base.BaseDto;
import cn.bbs.ba.log.LogTrac;
import cn.bbs.ba.path.BasePath;
import cn.bbs.ba.system.SystemKey;
import cn.bbs.ba.util.SqlStringUtil;
import cn.bbs.ba.util.XMLInfoPrint;

/**
 * ����ݿ�Ĳ����ѯ�����£��ύ���ع�
 * @author ������
 */

public class ADaoOperate extends DAOConnection {

	public static final String SQL_ID = "SQL_STANT";
	private static boolean bAutomFlag = true;
	private static Connection conn;

	/**
	 * ��ѯ��ݡ�
	 * @param sqlField HashMap
	 * @param key SystemKey
	 * @param pclass Class
	 * @return ���ز�ѯ���
	 */
	public static Fetcher search(HashMap<String, String> sqlField, SystemKey key, Class pclass) {
		// return list
		Fetcher fetch = null;
		try {
			String sqlID = sqlField.get(SQL_ID);
			HashMap<String, String> mapSqlValue = XMLInfoPrint.getXML(BasePath.getPath(key.name() + "DBSQLPath"));
			sqlField.put(SQL_ID, mapSqlValue.get("select_" + sqlID));
			String sqlW = SqlStringUtil.getSql(sqlField);
			DAOConnection._log.info("执行SQL\n" + sqlW);
			Statement stmt = getConnection(key).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			
			ResultSet rs = stmt.executeQuery(sqlW);
			rs.last();
			int cnt = rs.getRow();
			rs.beforeFirst();
			fetch = new DaoFetcher<BaseDto>(DAOConnection._log, rs, cnt, pclass);
		} catch (SQLException e) {
			DAOConnection._log.error("执行SQL错误");
			DAOConnection._log.error(e);
		}

		return fetch;
	}

	/**
	 * ������ݿ����
	 * @param sqlField HashMap
	 * @param key SystemKey
	 * @return int ���¼���
	 */
	public static int update(HashMap<String, String> sqlField, SystemKey key) {
		int iCnt = 0;
		try {
			String sqlID = sqlField.get(SQL_ID);
			HashMap<String, String> mapSqlValue = XMLInfoPrint.getXML(BasePath.getPath(key.name() + "DBSQLPath"));
			sqlField.put(SQL_ID, mapSqlValue.get("update_" + sqlID));
			String sqlW = SqlStringUtil.getSql(sqlField);
			LogTrac.info("更新SQL：\n" + sqlW);
			getConn(key);
			PreparedStatement stmt = conn.prepareStatement(sqlW);
			iCnt = stmt.executeUpdate();
		} catch (SQLException e) {
			LogTrac.error("更新SQL异常");
			DAOConnection._log.error(e);
			// 回滚
			ADaoOperate.rollback();
		}
		return iCnt;
	}

	private static void getConn(SystemKey key) {
		conn = getConnection(key);
		try {
			conn.setAutoCommit(bAutomFlag);
		} catch (SQLException e) {
			LogTrac.error("连接数据库异常");
			DAOConnection._log.error(e);
		}
	}

	/**
	 * �����Ƿ��Զ�����
	 * @param flag true:�Զ����� false�����Զ�����
	 */
	public static void setAutoCommit(boolean flag) {
		ADaoOperate.bAutomFlag = flag;
	}

	/**
	 * �Ը��µ�����ύ
	 */
	public static void commit() {
		if (conn != null) {
			try {
				conn.commit();
			} catch (SQLException e) {
				DAOConnection._log.error("�Զ��ύ����");
				DAOConnection._log.error(e);
			} 
		}
	}

	/**
	 * �Ը��µ���ݻع�
	 */
	public static void rollback() {
		if (conn != null) {
			try {
				conn.rollback();
			} catch (SQLException e) {
				DAOConnection._log.error("�ع����");
				DAOConnection._log.error(e);
			}
		}
	}
}
